window.ROOT = "";
(function() {

	jQuery('.addBtn').on('click', function() {
		window.location.href = window.rootPath + '/ui/news/addNews';
	});

	var serverData = {};
	var serverTable;
	jQuery(document)
			.ready(
					function() {
						serverTable = jQuery('#dataTable')
								.DataTable(
										{
											"serverSide" : true,
											"ordering" : false,
											"ajax" : {
												"dataType" : "json",
												"type" : "post",
												"cache" : false,
												"url" : window.rootPath
														+ '/ui/news/getpage',
												"data" : function(d) {
													return $.extend({}, d,
															serverData);
												}
											},
											columns : [
													{
														data : null,
														defaultContent : ""
													},
													{
														data : 'newsTitle'
													},
													{
														data : 'newsDescribe'
													},
													{
														data : 'user.name'
													},
													{
														data : 'createTime',
														render : function(data,
																type, full,
																meta) {
															if (data)
																return moment(
																		data)
																		.format(
																				'YYYY/MM/DD HH:mm');
															else
																return '';
														}
													},
													{
														data : null,
														defaultContent : "",
														render : function(data,
																type, full,
																meta) {
															var operate = ''
																	+ '<button type="button" class="btn btn-sm btn-info editBtn" title="编辑"><i class="fa fa-pencil"></i></button>'
																	+ '<button type="button" class="btn btn-sm btn-info delBtn" title="删除"><i class="fa fa-remove"></i></button>';

															return operate;

														}
													} ],
											"drawCallback" : function(oSettings) {
												var k = 0;
												var total = oSettings.aiDisplay.length
														+ oSettings._iDisplayStart;

												for (var i = oSettings._iDisplayStart, iLen = total; i < iLen; i++) {
													$(
															'td:eq(0)',
															oSettings.aoData[oSettings.aiDisplay[k]].nTr)
															.text(i + 1);
													k++;
												}

												serverTable.off('click', 'tr');
												serverTable
														.on(
																'click',
																'tr',
																function(event) {
																	event = event
																			|| window.event;
																	var target = event.target
																			|| event.scrElement;
																	var data = serverTable
																			.row(
																					this)
																			.data();

																	if (target.className
																			.indexOf('fa-remove') != -1
																			|| target.className
																					.indexOf('delBtn') != -1) {

																		deleteInfo(data);
																	}

																	if (target.className
																			.indexOf('fa-pencil') != -1
																			|| target.className
																					.indexOf('editBtn') != -1) {

																		editInfo(data);
																	}
																	return;
																});
											}
										});

						// 查询
						jQuery('.searchBtn').on('click', function() {
							serverData.key = jQuery('#search').val();
							serverTable.draw();
						})

						// 撤销查询
						jQuery('.resetBtn').on("click", function() {
							jQuery('#search').val('');
							serverData.key = '';
							serverTable.draw();
						});

						var deleteInfo = function(data) {
							if (!data)
								return;
							$.springLemonDialog({
								content : '确认删除新闻：' + data.newsTitle + '？',// 确认文本
								clickButton : function(sender, modal, index) {// 按钮事件处理
									// index:0 确认 1：取消
									// sender:事件源 如按钮，sender.html()可得到按钮文字如：取消
									if (index == 0) {
										var url = window.rootPath
												+ "/ui/news/delete/" + data.id;
										jQuery.ajax({
											url : encodeURI(url),
											cache : false,
											type : "post",
											dataType : "json",
											success : function(data) {
												serverTable.draw(false);
											}
										});
									}
									$(this).closeDialog(modal);
								}

							});

						};
						// 编辑信息
						var editInfo = function(data) {
							if (!data)
								return;
							window.location.href = window.rootPath
									+ '/ui/news/addNews?id=' + data.id;
						};

					})
})();